<?php
/**
 * @version		$Id: framework.php 37 2009-08-17 14:47:55Z naveen.gavini $
 * @package		Joomla
 * @subpackage	Joomla Multisite
 * @copyright	Copyright (C) 2009 Open Source Matters. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant to the
 * GNU General Public License, and as distributed it includes or is derivative
 * of works licensed under the GNU General Public License or other free or open 
 * source software licenses. See COPYRIGHT.php for copyright notices and
 * details.
 */

// No direct access.
defined('_JEXEC') or die;

/*
 * Joomla! system checks.
 */

@set_magic_quotes_runtime(0);
@ini_set('zend.ze1_compatibility_mode', '0');

/*
 * Installation check, and check on removal of the install directory.
 */

if (!file_exists(JPATH_CONFIGURATION.DS.'configuration.php') || (filesize(JPATH_CONFIGURATION.DS.'configuration.php') < 10) /*|| file_exists(JPATH_INSTALLATION.DS.'index.php')*/)
{
	if (file_exists(JPATH_INSTALLATION.DS.'index.php'))
	{
		header('Location: installation/index.php');
		exit();
	}
	else
	{
		echo 'No configuration file found and no installation code available. Exiting...';
		exit();
	}
}


/*
 * Joomla! system startup.
 */

// System includes.
require_once(JPATH_LIBRARIES.DS.'joomla'.DS.'import.php');

//BEGIN MULTSITE CODE
//get current url and store it into pageURL
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {
	$pageURL .= "s";
}
 
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
 	$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
 	$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}

$pageURL=str_replace("http://", "", $pageURL);
$pageURL=str_replace("https://", "", $pageURL);
$pageURL=str_replace("/", "", $pageURL);

//get list of config files
$dir=JPATH_ROOT.DS.'config';
$config_files;
if (is_dir($dir)) {
	if ($dh = opendir($dir)) {
    	$i=0;
        while (($file = readdir($dh)) !== false) {
        	if (($file != ".") && ($file != "..")) {
        		$config_files[$i]=$file;
        		$i=$i+1;
        	}
        }
        closedir($dh);
    }
}


//check if config exists for current url, if not use default configuration
$config_found=0;
$foundconfigs;
$z=0;
for($k=0; $k < count($config_files); $k++){
	if (strpos($pageURL, $config_files[$k]) !== false) {
		$config_found=1;
		$foundconfigs[$z]=$config_files[$k];
		$z++;
	}
}
if ($config_found==0){
	require_once(JPATH_CONFIGURATION.DS.'configuration.php');
}
if ($config_found==1){
	$len=0;
	$conf;
	for ($p=0; $p < count($foundconfigs); $p++){
		if (strlen($foundconfigs[$p]) > $len){
			$conf=$foundconfigs[$p];
		}
	}
	require_once(JPATH_ROOT.DS.config.'/'.$conf.'/configuration.php');
}

//END MULTISITE CODE

// System configuration.
$CONFIG = new JConfig();

if (@$CONFIG->error_reporting === 0) {
	error_reporting(0);
}
else if (@$CONFIG->error_reporting > 0)
{
	error_reporting($CONFIG->error_reporting);
	ini_set('display_errors', 1);
}

define('JDEBUG', $CONFIG->debug);

unset($CONFIG);

/*
 * Joomla! framework loading.
 */

// System profiler.
if (JDEBUG)
{
	jimport('joomla.error.profiler');
	$_PROFILER = &JProfiler::getInstance('Application');
}

// Joomla! library imports.
jimport('joomla.application.menu');
jimport('joomla.user.user');
jimport('joomla.environment.uri');
jimport('joomla.html.html');
jimport('joomla.utilities.utility');
jimport('joomla.event.event');
jimport('joomla.event.dispatcher');
jimport('joomla.language.language');
jimport('joomla.utilities.string');